package com.zkdcloud.aspectInterceptor.validatie;

import com.xdja.interceptor.exception.InvokeException;
import com.xdja.interceptor.util.ValidateUtils;
import com.zkdcloud.domain.Admin;
import com.zkdcloud.domain.Product;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Map;

import static com.zkdcloud.constant.Constants.HTTP_CONSTANTS.REQUEST_ADMIN;

/**
 * @author zk123
 * @version 2018/5/23
 */
@Component
public class ProductControllerValidate {
    /**
     * 添加product
     *
     * @param product product
     */
    public void add(Product product, Map<String,Object> map) throws InvokeException {
        ValidateUtils.assertCondition(!StringUtils.isEmpty(product.getName()),"名称必填");
        ValidateUtils.assertCondition(!StringUtils.isEmpty(product.getPrice()),"价格必填");
        ValidateUtils.assertCondition(!StringUtils.isEmpty(product.getDescription()),"描述必填");
        ValidateUtils.assertCondition(!StringUtils.isEmpty(product.getCategoryId()),"分类名必填");
        ValidateUtils.assertCondition(!StringUtils.isEmpty(product.getImageUrl()),"预览图必填");
        ValidateUtils.assertCondition(!StringUtils.isEmpty(product.getLeftNum()),"库存量必填");
        ValidateUtils.assertCondition(!StringUtils.isEmpty(product.getStatus()),"状态必填");

        Admin admin = (Admin) map.get(REQUEST_ADMIN);
        product.setBusinessId(admin.getBusinessId());
    }
}
